package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author : Jungle
 * @Description :
 */
@Configuration
public class MyRabbitConfig {
    /**
     * rabbitmq发送消息用json序列化,
     * 序列化后无需实现 Serializable  速度更快
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    //@RabbitListener(queues = {"stock.release.stock.queue"})
    //public void handle(Message message){
    //
    //}

    /**创建交换机，队列，绑定
     * 库存服务交换机
     */
    @Bean
    public Exchange stockEventExchange(){
        //(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
        return new TopicExchange("stock-event-exchange", true, false);
    }

    /**
     * 普通队列
     * @return
     */
    @Bean
    public Queue stockReleaseStockQueue(){
        //Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        return new Queue("stock.release.stock.queue",true,false,false);
    }


    /**
     * 延迟队列队列
     * @return
     */
    @Bean
    public Queue stockDelayQueue(){
        //Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        HashMap<String, Object> arguments = new HashMap<>();
        /**
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 60000
         */
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        arguments.put("x-dead-letter-routing-key", "stock.release");
        //单位ms
        arguments.put("x-message-ttl", 120000);
        return new Queue("stock.delay.queue",true,false,false, arguments);
    }

    /**
     * 消息刚发出经过交换机到延迟队列
     * 交换机绑定延迟队列
     * @return
     */
    @Bean
    public Binding stockLockedBinding(){
        //Binding(String destination, DestinationType destinationType, String exchange, String routingKey,Map<String, Object> arguments)
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }

    /**
     * 延迟队列到路由的消息，再发出用队列接收
     * 交换机绑定普通队列
     * @return
     */
    @Bean
    public Binding stockReleaseBinding(){
        //Binding(String destination, DestinationType destinationType, String exchange, String routingKey,Map<String, Object> arguments)
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",
                null);
    }
}
